package cn.javass.dp.composite.example1;

import java.util.ArrayList;
import java.util.Collection;

public class Composite {
    private Collection<Composite> childComposites = new ArrayList<>();
    private Collection<Leaf> childLeafs = new ArrayList<>();

    private String name = "";

    public Composite(String name) {
        this.name = name;
    }

    public void addComposite(Composite c) {
        childComposites.add(c);
    }

    public void addLeaf(Leaf l) {
        childLeafs.add(l);
    }

    public void printStruct(String preStr) {
        System.out.println("+"+this.name);
        preStr += " ";
        for (Leaf childLeaf : childLeafs) {
            childLeaf.printStruct(preStr);
        }
        for (Composite childComposite : childComposites) {
            childComposite.printStruct(preStr);
        }
    }
}
